<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{ item.pubdate | fromNow }}</span>
            <van-button @click="replyFn(item)" size="mini" type="default">
              回复 {{ item.reply_count }}
            </van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="likeFn(item)"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        >
        <van-button slot="button" size="mini" type="info" @click="publishFn">
          >发布
        </van-button>
      </van-field>
    </van-cell-group>
    <!-- 回复评论 -->
    <van-action-sheet v-model="show" title="评论回复">
      <CommentReply
        v-if="show"
        @addCommonCount="addCommonCount"
        :artId="artId"
        :currentCommon="currentCommon"
      ></CommentReply>
    </van-action-sheet>
  </div>
</template>

<script>
import CommentReply from './commentReply.vue'
import {
  getArticleCommon,
  addArticleCommon,
  likeCommon,
  unLikeCommon
} from '@/api/article'
export default {
  name: 'ArticleComment',
  props: {},
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, // 一开始加载第一个数据
      content: '', // 评论内容
      show: false,
      currentCommon: ''
    }
  },
  computed: {
    artId() {
      return this.$route.params.id
    }
  },
  components: {
    CommentReply
  },
  methods: {
    replyFn(item) {
      console.log('当前的评论 ', item)
      // 显示 action-sheet
      this.show = true
      this.currentCommon = item
    },
    async onLoad() {
      const res = await getArticleCommon(this.artId, this.offset)
      console.log(res)
      // 给素组赋值
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      // 给下一页的评论
      this.offset = res.data.last_id
      if (res.data.results.length === 0) {
        this.finished = true
      }
    },
    async publishFn() {
      // 发送请求
      if (this.content.length === 0) {
        return this.$toast('请输入文字')
      }
      const res = await addArticleCommon(this.artId, this.content)
      console.log(res)

      // 把res.data.new_obj 添加到list最前面
      this.list.unshift(res.data.new_obj)
      // 提示
      this.$toast('发布成功')
      // 清空输入框
      this.content = ''
    },
    // 评论点赞
    async likeFn(item) {
      // console.log(item)

      if (item.is_liking) {
        // 已经点赞了； ==>  取消点赞
        const res = await unLikeCommon(item.com_id)
        console.log(res)
      } else {
        // 点赞
        const res = await likeCommon(item.com_id)
        console.log(res)
      }
      this.$toast('操作成功')
      // 还要修改item.is_liking 的值，取反操作
      item.is_liking = !item.is_liking
    },
    addCommonCount() {
      // 子组件发布了评论的回复消息，需要在父组件中，让回复的数据+1
      this.currentCommon.reply_count++
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
